package com.price.compare.controller;


import com.price.compare.common.JsonResult;
import com.price.compare.dto.MaterialDTO;
import com.price.compare.entity.Material;
import com.price.compare.qo.MaterialQO;
import com.price.compare.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 物料表 前端控制器
 * </p>
 *
 * @author ccp
 * @since 2024-11-06
 */
@RestController
@RequestMapping("/material")
@Api(tags = "物料表")
public class MaterialController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private MaterialService materialService;

    /**
     * 物料表_分页查询
     */
    @ApiOperation(value = "物料表_分页查询:pageSize,pageNum,")
    @PostMapping(value = "/listPage")
    public JsonResult findListByPage(@RequestBody MaterialQO materialQO) {
        try {
            return materialService.queryPageList(materialQO);
        } catch (Exception e) {
            log.info("分页查询错误", e);
            return JsonResult.error(e.getMessage());
        }
    }

    /**
     * 物料表_分页查询
     *
     * @param materialQO
     * @return
     */
    @ApiOperation(value = "POA OR匹配_详情/新增_查询列表:bu,entity,poaCode,客户编码")
    @GetMapping(value = "/getList")
    public JsonResult getList(MaterialQO materialQO) {
        return materialService.getList(materialQO);
    }

    /**
     * 物料表_单个查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "物料表_单个查询")
    @GetMapping(value = "/getById", name = "单个查询")
    public JsonResult getById(Long id) {
        if (null == id) {
            return JsonResult.error("id不能为空");
        }
        Material material = materialService.getById(id);
        MaterialDTO materialDTO = new MaterialDTO();
        String materialHistoryMoney = material.getHistoryMoney();
        List<String> stringList = Arrays.asList(materialHistoryMoney.split(";"));
        List<BigDecimal> list = new ArrayList<>();
        for (int i = 0; i < stringList.size(); i++) {
            BigDecimal bigDecimal = new BigDecimal(stringList.get(i));
            list.add(bigDecimal);
        }
        return JsonResult.ok(materialDTO);
    }

    /**
     * 物料表_详情弹出框中新增
     *
     * @param material
     * @return
     */
    @ApiOperation(value = "物料表_单行新增")
    @PostMapping(value = "/add", name = "单行新增")
    public JsonResult add(@RequestBody Material material) {
        return materialService.add(material);
    }

    /**
     * 物料表_详情弹出框中编辑/保存_数据管理_修改数据
     *
     * @param material
     * @return
     */
    @ApiOperation(value = "物料表_单行保存")
    @PostMapping(value = "/update", name = "单行保存") // @Valid
    public JsonResult update(@RequestBody Material material) {
        return materialService.update(material);
    }

    /**
     * 物料表_单行删除_数据管理_删除数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "物料表_单行删除")
    @PostMapping(value = "/delete", name = "单行删除")
    public JsonResult batchDelete(Long id) {
        return materialService.delete(id);
    }

    /**
     * 物料表_勾选数据批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "物料表_批量删除")
    @PostMapping(value = "/batchDelete", name = "批量删除")
    public JsonResult batchDelete(@RequestBody List<Long> ids) {
        return materialService.batchDelete(ids);
    }

    /**
     * 物料表_导入_历史数据补充
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "物料表_上传excel")
    @PostMapping(value = "/uploadExcel", name = "数据收集上传")
    public JsonResult uploadExcel(@RequestParam("file") MultipartFile file) {
        try {
            //服务器下路径
            return materialService.uploadExcel(file);
        } catch (Exception e) {
            log.error("[Material]上传错误", e);
            return JsonResult.error("上传错误");
        }
    }

    /**
     * 物料表_导出
     *
     * @param response
     */
    @ApiOperation(value = "物料表_导出(bu,entity,poaCode)")
    @PostMapping(value = "/downloadData", name = "数据下载1")
    public void downloadData(@RequestBody MaterialQO materialQO, HttpServletResponse response) {
        materialService.downloadData(materialQO, response);
    }
}
